import { createRouter, createWebHistory } from 'vue-router'
import Login from '../views/Login.vue'
import Register from '../views/Register.vue'
import Home from '../views/Home.vue'

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  { path: '/register', component: Register },
  { path: '/home', component: Home },
  // { path: '/courses', component: Courses },
  // { path: '/schedule', component: Schedule }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})
router.beforeEach((to, from, next) => {
  const publicPages = ['/login', '/register']
  const userId = localStorage.getItem('userId')
  if (!publicPages.includes(to.path) && !userId) {
    next('/login')
  } else {
    next()
  }
})
export default router
